package com.ruoyi.system.service.module.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SmsTemplate;
import com.ruoyi.common.utils.SendSms;
import com.ruoyi.system.domain.module.estate.EstateRoom;
import com.ruoyi.system.mapper.module.SmsTemplateMapper;
import com.ruoyi.system.service.module.ISmsTemplateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 短信模板Service业务层处理
 *
 * @author ruoyi
 * @date 2022-06-19
 */
@Service
public class SmsTemplateServiceImpl extends ServiceImpl<SmsTemplateMapper, SmsTemplate> implements ISmsTemplateService {

	@Resource
	private SmsTemplateMapper smsTemplateMapper;

	@Override
	public void sendSmsByPropertyFeeToRoomUser(String phoneNum, EstateRoom room, String year1, String month1, String year2, String month2, BigDecimal money, String telephone) {
		//尊敬的${address}业主，您${room}户${year1}年${month1}月-${year2}年${month2}月物业费为(${money}元）。为保障小区服务质量，请尽快通过微信搜索精彩e家小程序，或至物业服务中心办理。祝您生活愉快，感谢配合！
		SmsTemplate sms = smsTemplateMapper.selectOneByColumn("sms_type",3);
		if(sms!=null){
			SendSms sendSms = new SendSms();
			//请求失败这里会抛ClientException异常
			JSONObject smsJson = new JSONObject();
			smsJson.set("address", room.getEstateName());
			smsJson.set("room", room.getRoomNo());
			smsJson.set("year1", year1);
			smsJson.set("month1", month1);
			smsJson.set("year2", year2);
			smsJson.set("month2", month2);
			smsJson.set("money", money);
//			smsJson.set("telephone", telephone);
			//短信请求数据=================：{"month1":"1","month2":"11","address":"测试小区","year1":"2024","year2":"2025","room":"1-1-601","money":22}
			System.out.println("短信请求数据=================：手机号：" + phoneNum + "：  "+  smsJson.toString());
			try {
				SendSmsResponse response = sendSms.send(phoneNum,smsJson,sms);
				//短信响应：{"code":"isv.SMS_TEMPLATE_ILLEGAL","message":"请检查模板内容与模板参数是否匹配","requestId":"25A574F0-4F6A-5068-83FC-336BBA4C9AAD"}
				//短信响应：{"code":"isv.SMS_TEMPLATE_ILLEGAL","message":"请检查模板内容与模板参数是否匹配","requestId":"6738D8F7-4CD8-507B-BAD9-D257EAF634E3"}
				//短信响应：{"code":"isv.SMS_TEMPLATE_ILLEGAL","message":"请检查模板内容与模板参数是否匹配","requestId":"2689084C-0966-5844-A6D2-4BBF5F6A1418"}
				//短信响应===============：{"code":"OK","message":"OK","requestId":"39546146-992C-5C8F-8294-FBABFA6127B9","bizId":"102614363004727084^0"}
				System.out.println("短信响应===============：手机号：" + phoneNum + "：  "+ JSONUtil.toJsonStr(response));
			} catch (ClientException e) {
				e.printStackTrace();
			}
		}
	}
}
